package org.xlp.mvc.context;

import org.xlp.bean.exception.BeanBaseException;

import java.lang.reflect.Type;

/**
 * 应用上下文工具类
 */
public class ApplicationContextUtils {
    /**
     * 应用上下文
     */
    private static ApplicationContext context;

    /**
     * 放入上下文
     * @param context
     */
    public static void pushContext(ApplicationContext context){
        ApplicationContextUtils.context = context;
    }

    /**
     * 获取上下文应用
     * @return
     */
    public static ApplicationContext getContext(){
        return ApplicationContextUtils.context;
    }

    /**
     * 获取该id的bean对象
     *
     * @param id bean id
     * @return bean 对象
     * @throws BeanBaseException 假如或bean过程失败，则抛出该异常
     */
    public static <T> T getBean(String id) throws BeanBaseException {
        return context.getBean(id);
    }

    /**
     * 获取该id的bean对象
     *
     * @param beanClass bean类型
     * @return bean 对象
     * @throws BeanBaseException 假如或bean过程失败，则抛出该异常
     */
    public static <T, I> T getBean(Class<I> beanClass) throws BeanBaseException {
        return context.getBean(beanClass);
    }

    /**
     * 获取该id的bean对象
     *
     * @param beanClass bean类型
     * @param types     目标泛型类型
     * @return bean 对象
     * @throws BeanBaseException 假如或bean过程失败，则抛出该异常
     */
    public static <T, I> T getBean(Class<I> beanClass, Type[] types) throws BeanBaseException {
        return context.getBean(beanClass, types);
    }

    /**
     * 获取该类全路径名称的bean对象
     *
     * @param className 类全路径名称
     * @return bean 对象
     * @throws BeanBaseException 假如或bean过程失败，则抛出该异常
     */
    public static <T> T getBeanByClassName(String className) throws BeanBaseException {
        return context.getBeanByClassName(className);
    }
}
